//
//  ByCellMine.m
//  chequanbao_ios
//
//  Created by mc on 2017/5/8.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByCellMine.h"

// vc
#import "ByUICardAndPayIndexVC.h"           // 我的二维码
#import "ByUIMineConsumeVC.h"               // 我的消费记录
#import "ByUIMinePointsVC.h"                // 我的积分
#import "ByUIMineSelectIDVC.h"              // 请选择身份
#import "ByUIMineWelfareVC.h"               // 我的福利
#import "ByUISafetyOrderRecordVC.h"         // 我的保险订单

@implementation ByCellMine
{
    UIView      *_bgView;
    
    UIImageView *_leftImg;
    UILabel     *_leftLabel;
    
    UIView      *_lineView;
    
    UIImageView *_rightImg;
    UILabel     *_rightLabel;
    
    NSArray     *_titlesAry;
    NSArray     *_imgAry;
    NSInteger   _section;
}

+ (instancetype)cellWithTableView:(UITableView*)tableView titlesAry:(NSArray *)titlesAry imgAry:(NSArray *)imgAry indexPath:(NSInteger)section{
    
    static NSString *cellIdentifier = @"ByCellMine";
    ByCellMine *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier titlesAry:titlesAry imgAry:imgAry indexPath:section];
    }
    return cell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier titlesAry:(NSArray *)titlesAry imgAry:(NSArray *)imgAry indexPath:(NSInteger)section{
    _titlesAry = titlesAry;
    _imgAry = imgAry;
    _section = section;

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createViews];
        self.selectionStyle = UITableViewCellSeparatorStyleNone;
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void)createViews{
    // 背景视图
    CGFloat x,y,width,height;
    x = 10;
    y = 0;
    width = SCREEN_WIDTH - 2*x;
    height = width/3;
    _bgView = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    _bgView.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.25];
    _bgView.layer.cornerRadius = 4.0;
    [self addSubview:_bgView];
    
    // 左侧背景
    x = 0;
    y = 0;
    width = (_bgView.width - 0.5)/2;
    height = _bgView.height;
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    leftView.tag = 10 + _section;
    leftView.backgroundColor = [UIColor clearColor];
    [_bgView addSubview:leftView];
    _bgView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(leftAction:)];
    [leftView addGestureRecognizer:tap];
    
    x = (_bgView.width/2 - 40)/2;
    y = (_bgView.height - 20 - 40 - 10)/2;
    width = height = 40;
    _leftImg = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    [_leftImg setImage:[UIImage imageNamed:_imgAry[0]]];
    [_bgView addSubview:_leftImg];
    
    x = 0;
    y = _leftImg.bottom + 10;
    width = (_bgView.width - 0.5)/2;
    height = 20;
    _leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    _leftLabel.textAlignment = NSTextAlignmentCenter;
    _leftLabel.font = FONT(14);
    _leftLabel.text = _titlesAry[0];
    _leftLabel.textColor = [UIColor whiteColor];
    [_bgView addSubview:_leftLabel];
    
    // 分割线
    x = _leftLabel.right;
    y = 5;
    width = 0.5;
    height = _bgView.height - 2*y;
    _lineView = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    _lineView.backgroundColor = [UIColor lightGrayColor];
    [_bgView addSubview:_lineView];
    
    // 右侧背景
    x = _lineView.right;
    y = 0;
    width = leftView.width;
    height = leftView.height;
    UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    rightView.userInteractionEnabled = YES;
    rightView.tag = 100 + _section;
    rightView.backgroundColor = [UIColor clearColor];
    [_bgView addSubview:rightView];
    
    x = _lineView.right + CGRectGetMinX(_leftImg.frame);
    y = CGRectGetMinY(_leftImg.frame);
    width = height = 40;
    _rightImg = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    [_rightImg setImage:[UIImage imageNamed:_imgAry[1]]];
    UITapGestureRecognizer *rightTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(rightAction:)];
    [rightView addGestureRecognizer:rightTap];
    [_bgView addSubview:_rightImg];
    
    
    x = _lineView.right;
    y = _rightImg.bottom + 10;
    width = _leftLabel.width;
    height = 20;
    _rightLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    _rightLabel.textAlignment = NSTextAlignmentCenter;
    _rightLabel.font = FONT(14);
    _rightLabel.text = _titlesAry[1];
    _rightLabel.textColor = [UIColor whiteColor];
    [_bgView addSubview:_rightLabel];
    

    
}
#pragma mark -- 点击事件
- (void)leftAction:(UITapGestureRecognizer *)tap{
    UIView *leftImg = (UIView *)tap.view;
    NSLog(@"%ld",(long)leftImg.tag);
    if (leftImg.tag != 11) {
        if (!BY_USER) {
            ByUILoginVC *vc = [[ByUILoginVC alloc] init];
            ByBaseNaviController *navi = [[ByBaseNaviController alloc] initWithRootViewController:vc];
            [self.viewController presentViewController:navi animated:YES completion:nil];
            return;
        }
    }
    // 我的二维码
    if (leftImg.tag == 10) {
        ByTabBarController *tab = [ByTabBarController shareSigleton];
        ByTabBarButton *button = (ByTabBarButton *)[tab.tabBarView viewWithTag:11];
        [tab _buttonAction:button];

    }
    // 我的福利
    else if (leftImg.tag == 11){
        ByUIMineWelfareVC *vc = [[ByUIMineWelfareVC alloc] init];
        [(ByBaseVC *)self.viewController by_normalPush:vc hasNavi:YES];
    }
    // 我的消费记录
    else{
        
        ByUIMineConsumeVC *vc = [[ByUIMineConsumeVC alloc] init];
        [(ByBaseVC *)self.viewController by_normalPush:vc hasNavi:YES];
    }
    
}

- (void)rightAction:(UITapGestureRecognizer *)tap{
    UIView *rightImg = (UIView *)tap.view;
    if (rightImg.tag != 102) {
        if (!BY_USER) {
            ByUILoginVC *vc = [[ByUILoginVC alloc] init];
            ByBaseNaviController *navi = [[ByBaseNaviController alloc] initWithRootViewController:vc];
            [self.viewController presentViewController:navi animated:YES completion:nil];
            return;
        }
    }
    // 我的保险订单
    if (rightImg.tag == 100) {
        ByUISafetyOrderRecordVC *vc = [[ByUISafetyOrderRecordVC alloc] init];
        [(ByBaseVC *)self.viewController by_normalPush:vc hasNavi:YES];
    }
    // 我的积分
    else if (rightImg.tag == 101){
        ByUIMinePointsVC *vc = [[ByUIMinePointsVC alloc] init];
        [(ByBaseVC *)self.viewController by_normalPush:vc hasNavi:YES];
    }
    // 推广中心
    else{
        ByUIMineSelectIDVC *vc = [[ByUIMineSelectIDVC alloc] init];
        [(ByBaseVC *)self.viewController by_normalPush:vc hasNavi:YES];
   
    }

}
@end
